package com.ruoyi.js.controller;

import java.io.IOException;
import java.util.List;

import com.ruoyi.common.config.Global;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.js.domain.JsMatch;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.js.domain.JsJudge;
import com.ruoyi.js.service.IJsJudgeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 裁判 Controller
 * 
 * @author greejoy
 * @date 2024-10-16
 */
@Controller
@RequestMapping("/js/judge")
public class JsJudgeController extends BaseController
{
    private String prefix = "js/judge";

    @Autowired
    private IJsJudgeService jsJudgeService;

    @RequiresPermissions("js:judge:view")
    @GetMapping()
    public String judge()
    {
        return prefix + "/judge";
    }

    /**
     * 查询裁判 列表
     */
    @RequiresPermissions("js:judge:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(@CookieValue("app_match_id") Long matchId, JsJudge jsJudge)
    {
        jsJudge.setMatchId(matchId);
        startPage();
        List<JsJudge> list = jsJudgeService.selectJsJudgeList(jsJudge);
        return getDataTable(list);
    }

    /**
     * 导出裁判 列表
     */
    @RequiresPermissions("js:judge:export")
    @Log(title = "裁判 ", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JsJudge jsJudge)
    {
        List<JsJudge> list = jsJudgeService.selectJsJudgeList(jsJudge);
        ExcelUtil<JsJudge> util = new ExcelUtil<JsJudge>(JsJudge.class);
        return util.exportExcel(list, "judge");
    }

    /**
     * 新增裁判 
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存裁判 
     */
    @RequiresPermissions("js:judge:add")
    @Log(title = "裁判 ", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@CookieValue("app_match_id") Long matchId, JsJudge jsJudge, MultipartFile photoFile)
    {
        jsJudge.setMatchId(matchId);
        buildJudgeFiles(jsJudge, photoFile);
        return toAjax(jsJudgeService.insertJsJudge(jsJudge));
    }

    /**
     * 修改裁判 
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        JsJudge jsJudge = jsJudgeService.selectJsJudgeById(id);
        mmap.put("jsJudge", jsJudge);
        return prefix + "/edit";
    }

    /**
     * 修改保存裁判 
     */
    @RequiresPermissions("js:judge:edit")
    @Log(title = "裁判 ", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JsJudge jsJudge, MultipartFile photoFile)
    {
        buildJudgeFiles(jsJudge, photoFile);
        return toAjax(jsJudgeService.updateJsJudge(jsJudge));
    }

    /**
     * 删除裁判 
     */
    @RequiresPermissions("js:judge:remove")
    @Log(title = "裁判 ", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jsJudgeService.deleteJsJudgeByIds(ids));
    }

    private void buildJudgeFiles(JsJudge judge, MultipartFile photoFile){
        // 上传文件路径
        String filePath = Global.getUploadPath();
        if(photoFile != null){
            String photo = null;
            try {
                photo = FileUploadUtils.upload(filePath, photoFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
            judge.setPhoto(photo);
        }
    }

}
